PYTHON ?= python3
PROJECT_NAME=anonymizer
COVERAGE_PERCENT=70

JOBS ?= 4
ifeq ($(SHORT),)
	CODE = src tests
else
    CODE = $(shell git status -s --porcelain -uno | awk 'match($$1, "[RAM]+"){$$1=""; print $$0}' | sed 's/.* -> //' | grep --extended-regexp --color=no '\.py$$')
endif


ifeq ($(wildcard .env),)
$(info Creating .env file from example.env...)
$(shell cp example.env .env)
endif
include .env

export DOCKER_CLI_EXPERIMENTAL=enabled
export PIP_INDEX_URL?=https://pypi.org/simple/
export PYTHON_IMAGE = python:$(shell cat .python-version)-slim
export USER_UID=5453

.PHONY: lint test

pull-%:
	docker-compose pull $(subst pull-,,$@) # Always success, even if no image exist!

build-%:
	docker-compose build $(subst build-,,$@)

up-%:
	docker-compose up $(subst up-,,$@)

down-%:
	docker-compose down $(subst down-,,$@)

push-%:
	docker-compose push $(subst push-,,$@)

docker-tests:
	docker-compose run test make tests-unit
	docker-compose run test make tests

docker-lint:
	docker-compose run test make lint

security:
	-pip-audit

black-lint:
	-black --check ${CODE}

pylint:
	pylint --jobs $(JOBS) --rcfile=setup.cfg $${CI:+--output-format=pylint_gitlab.GitlabCodeClimateReporter --output=codeclimate-pylint.json} $(CODE)

lint: black-lint
	-@mypy ${CODE}
	-@flake8 ${CODE}
	-toml-sort --check pyproject.toml
	-pylint --jobs $(JOBS) --rcfile=setup.cfg $${CI:+--output-format=pylint_gitlab.GitlabCodeClimateReporter --output=codeclimate-pylint.json} $(CODE)

pretty:
	-isort ${CODE}
	-black ${CODE}
	-toml-sort --in-place --all pyproject.toml

tests-%:
	coverage run -m pytest $${CI:+--junitxml=report-$(subst tests-,,$@).xml} tests/$(subst tests-,,$@)/
	mv .coverage .coverage.$(subst tests-,,$@)

tests: tests-unit
	coverage combine
	[ -n $$CI ] && coverage xml || true # always success
	coverage report --fail-under=85

